package py.com.alfa.escriba.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;


public class ObjectUtils {
	
	private static Logger logger = LoggerFactory.getLogger(ObjectUtils.class);
	
	public static void copyValues(Object source, Object target){
		try{
			BeanUtils.copyProperties(source, target);
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public static File byteToFile(byte[] data) throws IOException {
    String tmpFilename = UUID.randomUUID().toString();
    File file = File.createTempFile(tmpFilename, null);
    file.deleteOnExit();
    OutputStream out = new FileOutputStream(file);
    out.write(data);
    out.close();
    return file;
  }
  
  public static File byteToFile(byte[] data, String type) throws IOException {
    String tmpFilename = UUID.randomUUID().toString();
    File file = File.createTempFile(tmpFilename, type);
    file.deleteOnExit();
    OutputStream out = new FileOutputStream(file);
    out.write(data);
    out.close();
    return file;
  }
	
	public static boolean isEmpty(Object obj) {
		logger.info("OBJECT: {}", obj);
		if (obj == null) {
			return true;
		}

		if (obj instanceof String) {
			String cadena = (String) obj;
			if (cadena == null || cadena.equals(""))
				return true;
			else
				return false;
		} else if (obj instanceof Long) {
			Long entero = (Long) obj;
			if (entero == null || entero.compareTo(0L) == 0)
				return true;
			else
				return false;
		} else {
			return false;
		}
	}
	
	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}

	 public static String parseString(String s){
//		return StringUtils.upperCase(StringUtils.trimToNull(s));
		return StringUtils.upperCase(StringUtils.trim(s));

	}	
	 
	public static String encryptMD5(String clave) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("MD5");
		byte[] b = md.digest(clave.getBytes());
		int size = b.length;
		StringBuffer h = new StringBuffer(size);
		for (int i = 0; i < size; i++) {
		   int u = b[i] & 255;
		   if (u < 16) {
		      h.append("0" + Integer.toHexString(u));
		   } else {
		      h.append(Integer.toHexString(u));
		   }
		}
		return h.toString();
	}
	
	public static String cleanChar(String str, char character) {
		 for(int i=0;i<str.length();i++) {
			 if( str.charAt(i) != character) {
				 return str.substring(i);
			 }
		 }	     
		 return str;
	}
	
	public static boolean validatePwd(String password) {
		Pattern pattern;
		Matcher matcher;
		
		pattern = Pattern.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,20})");
		
		matcher = pattern.matcher(password);
		return matcher.matches();
	}
	
}
